/******************************************************************************\
    Time.cpp contains functions for working with time.
    Copyright (C) 2008  Victor Vinogradov (fly3333@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
\******************************************************************************/

#include ".\Time.h"
#include <ctime>
#include <windows.h>

unsigned long long GetTime()                    
{
	SYSTEMTIME local_system_time;
	unsigned long long time;
	GetLocalTime( &local_system_time );
	if( !MakeTime( &time, local_system_time.wYear, local_system_time.wMonth, local_system_time.wDay, local_system_time.wHour, local_system_time.wMinute, local_system_time.wSecond, local_system_time.wMilliseconds ) )
		return 0;
	return time;
}

bool SplitTime( unsigned long long time, unsigned short *year, unsigned short *month, unsigned short *day, unsigned short *day_of_week, unsigned short *hour, unsigned short *minute, unsigned short *second, unsigned short *millisecond )
{
	//SYSTEMTIME system_time_2000, current_system_time;
	//FILETIME file_time_2000, local_file_time_2000;
	//ULARGE_INTEGER current_time;

	//system_time_2000.wYear = 2000;
	//system_time_2000.wMonth = 1;
	//system_time_2000.wDay = 1;
	//system_time_2000.wHour = 0;
	//system_time_2000.wMinute = 0;
	//system_time_2000.wSecond = 0;
	//system_time_2000.wMilliseconds = 0;
	//if( !SystemTimeToFileTime( &system_time_2000, &file_time_2000 ) )
	//	return false;
	//if( !FileTimeToLocalFileTime( &file_time_2000, &local_file_time_2000 ) )
	//	return false;

	//current_time.QuadPart = ( ULONGLONG )( ( ULONGLONG )time * 10000 + ( ( ULARGE_INTEGER * )&local_file_time_2000 )->QuadPart );
	//if( !FileTimeToSystemTime( ( FILETIME * )&current_time, &current_system_time ) )
	//	return false;

	SYSTEMTIME system_time;
	FILETIME file_time;
	ULARGE_INTEGER large_time;

	large_time.QuadPart = time;

	file_time.dwHighDateTime = large_time.HighPart;
	file_time.dwLowDateTime = large_time.LowPart;

	if( !FileTimeToSystemTime( &file_time, &system_time ) )
		return false;

	if( year != NULL )
		*year = system_time.wYear;
	if( month != NULL )
		*month = system_time.wMonth;
	if( day != NULL )
		*day = system_time.wDay;
	if( day_of_week != NULL )
		*day_of_week = system_time.wDayOfWeek;
	if( hour != NULL )
		*hour = system_time.wHour;
	if( minute != NULL )
		*minute = system_time.wMinute;
	if( second != NULL )
		*second = system_time.wSecond;
	if( millisecond != NULL )
		*millisecond = system_time.wMilliseconds;
	return true;
}

bool MakeTime( unsigned long long *time, unsigned short year, unsigned short month, unsigned short day, unsigned short hour, unsigned short minute, unsigned short second, unsigned short millisecond )
{
	//SYSTEMTIME system_time_2000, current_system_time;
	//FILETIME file_time_2000, local_file_time_2000, current_file_time;

	//system_time_2000.wYear = 2000;
	//system_time_2000.wMonth = 1;
	//system_time_2000.wDay = 1;
	//system_time_2000.wHour = 0;
	//system_time_2000.wMinute = 0;
	//system_time_2000.wSecond = 0;
	//system_time_2000.wMilliseconds = 0;
	//if( !SystemTimeToFileTime( &system_time_2000, &file_time_2000 ) )
	//	return false;
	//if( !FileTimeToLocalFileTime( &file_time_2000, &local_file_time_2000 ) )
	//	return false;

	SYSTEMTIME system_time;
	FILETIME file_time;
	ULARGE_INTEGER large_time;

	system_time.wYear = ( unsigned short )year;
	system_time.wMonth = ( unsigned short )month;
	system_time.wDay = ( unsigned short )day;
	system_time.wHour = ( unsigned short )hour;
	system_time.wMinute = ( unsigned short )minute;
	system_time.wSecond = ( unsigned short )second;
	system_time.wMilliseconds = ( unsigned short )millisecond;
	if( !SystemTimeToFileTime( &system_time, &file_time ) )
		return false;

	large_time.HighPart = file_time.dwHighDateTime;
	large_time.LowPart = file_time.dwLowDateTime;

	if( time != NULL )
		*time = large_time.QuadPart;
	return true;
}
